package cn.dt70.controller;

import cn.dt70.pojo.AqbInterest;
import cn.dt70.pojo.AqbInterestandcode;
import cn.dt70.pojo.AqbUser;
import cn.dt70.service.AqbInterestService;
import cn.dt70.service.AqbInterestandcodeService;
import cn.dt70.utils.CompareDate;
import cn.dt70.utils.Constants;
import cn.dt70.utils.DateUtils;
import cn.dt70.utils.PageSupport;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/zyf/")
public class Controllerzyf {


    @Resource
    private AqbInterestService aqbInterestService;

    @Resource
    private AqbInterestandcodeService aqbInterestandcodeService;

    @RequestMapping("todo")
    public String todo() {
        System.out.println("----进入控制器！----");
        return "product";
    }

    @RequestMapping("show")
    public String show() {
        System.out.println("----进入控制器！----");
        return "usercenter-rewardcontrol-interestcoupon";
    }

    @RequestMapping("showJxj")
    @ResponseBody//显示加息卷，附带分页和状态查询
    public String showJxj(@Param("status") String status, PageSupport ps, HttpSession session,
                          @Param("currentPageNo") String currentPageNo) {
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);//获取对象
        if (null == status || "".equals(status)) {//判断是否为空
            status = "0";
        }
        if (Integer.parseInt(status) == 1) {//根据加息卷天数更新加息卷状态（是否到期）
            changeStatus(aqbUser, status);
        }
        if (null == currentPageNo || "".equals(currentPageNo)) {
            ps.setCurrentPageNo(0);
        } else {
            ps.setCurrentPageNo(Integer.parseInt(currentPageNo));
        }
        List<AqbInterest> aqbInterestList = aqbInterestService.getAllByUid(aqbUser.getUserId(), Integer.valueOf(status) - 1, ps);
        String aqbInterests = JSON.toJSONString(aqbInterestList);
        String phs = JSON.toJSONString(ps);
        StringBuffer sb = new StringBuffer("{");
        sb.append("\"aqbInterests\":" + aqbInterests + "");
        sb.append(",\"phs\":" + phs + "");
        sb.append(",\"status\":" + status + "");
        sb.append("}");
        return sb.toString();
    }

    @RequestMapping("conversion")
    @ResponseBody//加息码兑换加息卷
    public String conversion(@Param("rewardcode") String rewardcode, HttpSession session) {
        String flag = "";
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        AqbInterestandcode aqbInterestandcode = aqbInterestandcodeService.getJxj(rewardcode);
        if (aqbInterestService.addInterest(aqbUser.getUserId(), aqbInterestandcode)) {
            if (aqbInterestandcodeService.updateStatus(rewardcode)) {
                flag = "t";
            } else {
                flag = "h";
            }
        } else {
            flag = "s";
        }
        return flag;
    }

    @RequestMapping("showInterest")
    @ResponseBody//使用选择加息劵页面的查询
    public String showInterest(HttpSession session,
                               @Param("maxMoney")String maxMoney) {
        if(null == maxMoney || "".equals(maxMoney)){
            maxMoney = "0";
        }
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        changeStatus(aqbUser, "1");//判断加息卷是否到期
        List<AqbInterest> aqbInterestList = aqbInterestService.getInterestsByUidAndMoney(aqbUser.getUserId(), 0,Double.parseDouble(maxMoney));
        StringBuffer sb = new StringBuffer("{");
        sb.append("\"aqbInterestList\":" + JSON.toJSONString(aqbInterestList) + "");
        sb.append(",\"aqbUser\":" + JSON.toJSONString(aqbUser) + "");
        sb.append("}");
        return sb.toString();
    }

    //判断加息卷是否到期，并改变状态
    public void changeStatus(AqbUser aqbUser, String status) {
        List<AqbInterest> aqbInterests = aqbInterestService.getInterestsByUid(aqbUser.getUserId(), Integer.valueOf(status) - 1);
        for (AqbInterest ai : aqbInterests) {
            String date1 = CompareDate.getResultDate(DateUtils.format(ai.getCreateTime()), ai.getTime().intValue());
            String date2 = CompareDate.getResult(0);
            if (CompareDate.compare(date1, date2)) {
                if (aqbInterestService.changeStatus(aqbUser.getUserId(), ai.getJxId(), 2)) {
                    System.out.println("用户id为：" + ai.getJxId() + "的加息卷到期，自动执行到期代码");
                }
            }
        }
    }
}
